<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\bill;

use app\common\dao\bill\BillFinanceDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;
use app\common\repositories\property\basic\PropertyBasicRepository;

/**
 * Class BillFinanceRepository
 *
 * @mixin BillFinanceDao
 */
class BillFinanceRepository extends BaseRepository
{
    const PAY_METHOD = [
        1 => '企业微信',
        2 => '微信',
        3 => '支付宝',
        4 => '银联',
        5 => '现金',
    ];

    const PAY_TYPE = [
        1 => '线上支付',
        2 => '线下支付',
    ];

    public function __construct(BillFinanceDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'roomInfo' => function ($query) {
                    $query->bind(['full_room_name']);
                },
                'cateInfo' => function ($query) {
                    $query->bind(['full_cate_name']);
                },
                'propertyInfo' => function ($query) {
                    $query->field('id,name');
                    $query->bind(['property_name' => 'name']);
                },
            ])
            ->order('id', 'desc')
            ->select();
        return compact('list', 'count');
    }


    public function addInfo(int $propertyId, array $data, int $userId = 0)
    {
        $data['property_id'] = $propertyId;
        return $this->dao->create($data);
    }


    /**
     * 编辑
     */
    public function editInfo($info, array $data, int $userId = 0)
    {
        return $this->dao->update($info['id'], $data);
    }

    public function getDetail(int $id)
    {
        $data = $this->dao->search([])
            ->with([
                'cateInfo' => function ($query) {
                    $query->bind(['full_cate_name']);
                },
                'roomInfo' => function ($query) {
                    $query->bind(['full_room_name']);
                }
            ])
            ->where('id', $id)
            ->find();
        return $data;
    }


    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'BILL.FINANCE.DEL', $info);
        $this->dao->delete($info['id']);
    }


    /**
     * 生成流水单号
     */
    public function getFinanceNo($propertyId = 0)
    {
        if ($propertyId > 0) {
            /** @var PropertyBasicRepository $propertyBasicRepository */
            $propertyBasicRepository = app()->make(PropertyBasicRepository::class);
            $codeInfo = $propertyBasicRepository->getSearch(['id' => $propertyId])->value('code');
        } else {
            $codeInfo = 'AAAAA';
        }
        return 'F' . date('ymd') . date('His') . rand(1111, 9999) . $codeInfo;
    }


    public function rechargeAdd($meterInfo, $data, $userId = 0)
    {
        $remark = '';
        if ($data['is_type'] == 1) {
            $remark = '充值';
        }
        if ($data['is_type'] == 2) {
            $remark = '退款';
        }

        /** @var BillCateRepository $billCateRepository */
        $billCateRepository = app()->make(BillCateRepository::class);

        $financeData['user_id'] = $userId;
        $financeData['is_type'] = $data['is_type'];
        $financeData['start_date'] = date("Y-m-d");
        $financeData['month_time'] = date('Y-m');
        $financeData['end_date'] = date("Y-m-d");
        $financeData['plan_type'] = $data['is_type']; ## 减少 即 预付费退款
        $financeData['amount'] = $data['pay_amount'];
        $financeData['energy_id'] = $meterInfo['id'];
        $financeData['pay_type'] = $data['pay_type'];
        $financeData['pay_method'] = $data['pay_method'];
        $financeData['room_id'] = $meterInfo['room_id'] ?? '0';
        $financeData['property_id'] = $meterInfo['property_id'] ?? '0';
        $financeData['finance_no'] = $this->getFinanceNo($meterInfo['property_id']);
        $financeData['cate_id'] = $billCateRepository->getBillCateId( ['is_class' => 2, 'set_finance' => $meterInfo['energy_type']]);
        $financeData['remark'] = $meterInfo['energy_name'] . ($billCateRepository::IS_ENERGY[$meterInfo['energy_type']] ?? '') . $remark . '&' . $meterInfo['energy_no'];
        $this->dao->create($financeData);
    }
}